﻿/*
 * Copyright (c) Seagull Consulting, Inc. All rights reserved. See License.txt in the project root for license information.
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace SeagullConsulting.MvcAjaxControls.Web.Mvc.Helpers
{

    #region Interfaces
    public interface IGridColumn<T> where T : class
    {
        string Header { get; set; }
        string PropertyName { get; set; }
        ColumnFormats Format { get; set; }
        bool FilterEnabled { get; set; }
    }

    public interface IGridActionColumn<T> where T : class
    {
        string Text { get; set; }
        string Action { get; set; }
        string Controller { get; set; }
        string ImageUrl { get; set; }
        string Id { get; set; }
    }
    #endregion

    #region GridColumn
    /// <summary>
    /// Represents a column in a AjaxGrid.
    /// </summary>
    /// <typeparam name="T">Class that is rendered in a table.</typeparam>
    /// <typeparam name="TProp">Class property that is rendered in the column.</typeparam>
    public class GridColumn<T, TProp> : IGridColumn<T>
        where T : class
    {
        /// <summary>
        /// Column header to display.
        /// </summary>
        public string Header { get; set; }

        /// <summary>
        /// Column Property Name.
        /// </summary>
        public string PropertyName { get; set; }

        /// <summary>
        /// Column Format Name.
        /// </summary>
        public ColumnFormats Format { get; set; }

        /// <summary>
        /// Column Filtering Enabled.
        /// </summary>
        public bool FilterEnabled { get; set; }

        /// <summary>
        /// Constructors.
        /// </summary>
        /// <param name="expression">Lambda expression identifying a property to be rendered.</param>

        //Constructor for data column
        public GridColumn(Expression<Func<T, TProp>> expression, ColumnFormats format, bool filterEnabled)
        {
            InitializeColumn(String.Empty, expression, format, filterEnabled);
        }

        public GridColumn(string header, Expression<Func<T, TProp>> expression, ColumnFormats format, bool filterEnabled)
        {
            InitializeColumn(header, expression, format, filterEnabled);
        }

        private void InitializeColumn(string header, Expression<Func<T, TProp>> expression, ColumnFormats format, bool filterEnabled)
        {
            string expressionBody = expression.Body.ToString();
            PropertyName = expressionBody.Substring(expressionBody.IndexOf('.') + 1);
            Header = PropertyName;
            if (header.Length > 0)
                Header = header;
            Format = format;
            FilterEnabled = filterEnabled;
        }
    }
    #endregion


    #region GridActionColumn
    /// <summary>
    /// Represents an action column in a AjaxGrid.
    /// </summary>
    /// <typeparam name="T">Class that is rendered in a table.</typeparam>
    public class GridActionColumn<T, TProp> : IGridActionColumn<T>
        where T : class
    {
        /// <summary>
        /// Action text to display.
        /// </summary>
        public string Text { get; set; }

        /// <summary>
        /// Action name.
        /// </summary>
        public string Action { get; set; }

        /// <summary>
        /// Controller name.
        /// </summary>
        public string Controller { get; set; }

        /// <summary>
        /// Action name.
        /// </summary>
        public string ImageUrl { get; set; }

        /// <summary>
        /// Id key property value.
        /// </summary>
        public string Id { get; set; }

        /// <summary>
        /// Constructors.
        /// </summary>
        /// <param name="expression">Lambda expression identifying a property to be rendered.</param>

        //Constructor for action column
        public GridActionColumn(string text, string action, Expression<Func<T, TProp>> expression)
        {
            Text = text;
            Action = action;
            Controller = String.Empty;
            ImageUrl = String.Empty;
            string expressionBody = expression.Body.ToString();
            Id = expressionBody.Substring(expressionBody.IndexOf('.') + 1);
        }
        public GridActionColumn(string text, string action, string controller, Expression<Func<T, TProp>> expression)
        {
            Text = text;
            Action = action;
            Controller = controller;
            ImageUrl = String.Empty;
            string expressionBody = expression.Body.ToString();
            Id = expressionBody.Substring(expressionBody.IndexOf('.') + 1);
        }
        public GridActionColumn(string text, string action, Expression<Func<T, TProp>> expression, string imageUrl)
        {
            Text = text;
            Action = action;
            Controller = String.Empty;
            ImageUrl = imageUrl;
            string expressionBody = expression.Body.ToString();
            Id = expressionBody.Substring(expressionBody.IndexOf('.') + 1);
        }
        public GridActionColumn(string text, string action, string controller, Expression<Func<T, TProp>> expression, string imageUrl)
        {
            Text = text;
            Action = action;
            Controller = controller;
            ImageUrl = imageUrl;
            string expressionBody = expression.Body.ToString();
            Id = expressionBody.Substring(expressionBody.IndexOf('.') + 1);
        }
    }
    #endregion
}
